package com.qdsg.ysg.doctor.ui;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.qdsg.ysg.doctor.ui.dialog.TeamDoctorDialog;
import com.qdsg.ysg.doctor.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.response.BaseResponse;
import com.rest.response.DoctorResponse;
import com.rest.response.TeamDetailResponse;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseActivity;

public class TeamManageActivity extends BaseActivity {
    @BindView(R.id.edt_mould_name)
    EditText edt_mould_name;
    @BindView(R.id.edt_advice)
    EditText edt_advice;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    String id;
    List<DoctorResponse.Doctor> list = new ArrayList<>();
    MyAdapter adapter;

    @Override
    protected void initViews() {
        LinearLayoutManager manager = new LinearLayoutManager( TeamManageActivity.this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        adapter = new MyAdapter();
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.bottom = 1;
            }
        });
    }

    @Override
    protected void initData() {
        id = getIntent().getStringExtra("id");
        getTeamDetail();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.aty_team_manage;
    }

    @OnClick(R.id.btn_add)
    void btn_add() {
        showDialog();
    }

    @OnClick(R.id.btn_confirm)
    void btn_confirm(){
        updateTeamDetail();
    }


    private TeamDoctorDialog mTeamDoctorDialog = null;


    private void showDialog() {
        // 初始化点击头像弹出的对话框
        mTeamDoctorDialog = TeamDoctorDialog.newInstance();
        mTeamDoctorDialog.setOnItemClickListener(new TeamDoctorDialog.OnItemClickListener() {
            @Override
            public void onItem(DoctorResponse.Doctor doctor) {
                list.add(doctor);
                addTeamMember(id, doctor.id);
                adapter.notifyDataSetChanged();
                mTeamDoctorDialog.dismiss();
            }
        });
        mTeamDoctorDialog.show(getSupportFragmentManager(), "TeamDoctorDialog");
    }

    class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {

        @Override
        public MyAdapter.Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            MyAdapter.Holder myHolder;
            myHolder = new MyAdapter.Holder(LayoutInflater.from( TeamManageActivity.this).inflate(R.layout.item_team_doctor, parent, false));
            return myHolder;
        }

        @Override
        public void onBindViewHolder(MyAdapter.Holder holder, final int position) {
            holder.tv_name.setText(list.get(position).docName);
            holder.tv_job.setText(list.get(position).jobTitleName);
            if (list.get(position).memberType == 0) {
                holder.tv_type.setText("成员");
                holder.btn_del.setVisibility(View.VISIBLE);
            } else {
                holder.tv_type.setText("负责人");
                holder.btn_del.setVisibility(View.GONE);
            }
            holder.btn_del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    delTeamMember(id, list.get(position).id);
                    list.remove(position);
                    adapter.notifyDataSetChanged();
                }
            });
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            TextView tv_name, tv_job, tv_type;
            ImageView btn_del;

            public Holder(View itemView) {
                super(itemView);
                tv_name = itemView.findViewById(R.id.tv_name);
                tv_job = itemView.findViewById(R.id.tv_job);
                tv_type = itemView.findViewById(R.id.tv_type);
                btn_del = itemView.findViewById(R.id.btn_del);
            }
        }
    }

    private void getTeamDetail() {
        RestProxy.getInstance().getTeamDetail(id, new Observer<TeamDetailResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(TeamDetailResponse teamDetailResponse) {
                edt_mould_name.setText(teamDetailResponse.data.name);
                edt_advice.setText(teamDetailResponse.data.description);
                list.addAll(teamDetailResponse.data.detailDtoList);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError( TeamManageActivity.this, e);
            }

            @Override
            public void onComplete() {

            }
        });
    }


    private void addTeamMember(String id, String doctorId) {
        RestProxy.getInstance().addTeamMember(id, doctorId, new Observer<BaseResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseResponse baseResponse) {

            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError( TeamManageActivity.this, e);
            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void delTeamMember(String id, String doctorId) {
        RestProxy.getInstance().delTeamMember(id, doctorId, new Observer<BaseResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseResponse baseResponse) {

            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError( TeamManageActivity.this, e);
            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void updateTeamDetail() {
        RestProxy.getInstance().updateTeamDetail(id, edt_mould_name.getText().toString(), edt_advice.getText().toString(), new Observer<BaseResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                finish();
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError( TeamManageActivity.this, e);
            }

            @Override
            public void onComplete() {

            }
        });
    }
}
